﻿@inject IDataCacheService _dataCacheService


<MDCard Outline Class="masa-calendar-card">
    <div class="title-card ps-4 pe-4 pb-2">
        <div class="title-large">
            @Month.ToString("yyyy年M月")
        </div>
        <div class="button-card">
            <MButton Text  OnClick="@(()=>ValueChanged(0))" Class="me-4" Color="primary">
                上一年
            </MButton>
            <MButton Text OnClick="@(()=>ValueChanged(1))" Class="me-4" Color="primary">
                上一月
            </MButton>
            <MButton Text OnClick="@(()=>ValueChanged(2))" Class="me-4" Color="primary">
                今天
            </MButton>
            <MButton Text OnClick="@(()=>ValueChanged(3))" Class="me-4" Color="primary">
                下一月
            </MButton>
            <MButton Text OnClick="@(()=>ValueChanged(4))" Class="me-4" Color="primary">
                下一年
            </MButton>
        </div>
    </div>
    <div class="week-card">
        <div>日</div>
        <div>一</div>
        <div>二</div>
        <div>三</div>
        <div>四</div>
        <div>五</div>
        <div>六</div>
    </div>
    @for (var k = 0; k < 6; k++)
    {
        <div class="month-card">
            @for (int i = 0; i < 7; i++)
            {
                var date = Begin.AddDays(k * 7 + i);
                <div class="day-card @((date.Month==Month.Month?"":"active")) @((i==0?"first":""))">
                  @ChildContent(date)
                </div>
            }
        </div>
    }
</MDCard>


@code {
    [Parameter]
    public DateTime Month { get; set; }
    [Parameter]
    public EventCallback<DateTime> MonthChanged { get; set; }
    [Parameter]
    public RenderFragment<DateTime> ChildContent { get; set; }
    /// <summary>
    /// 起始日期
    /// </summary>
    public DateTime Begin { get; set; }

    /// <summary>
    /// 结束日期
    /// </summary>
    public DateTime End { get; set; }

    public void Refresh()
    {
        //根据当前月份计算出显示范围
        var temp = Month.AddDays(-Month.Day + 1);

        Begin = temp.AddDays(-(int)temp.DayOfWeek);
        End = Begin.AddDays(42);

        StateHasChanged();

        
    }

    protected override void OnParametersSet()
    {
        Refresh();
    }

    public async Task ValueChanged(StringNumber index)
    {
        switch (index?.ToInt32())
        {
            case 0:
                Month = Month.AddYears(-1);
                break;
            case 1:
                Month = Month.AddMonths(-1);
                break;
            case 2:
                Month = DateTime.Now.ToCstTime();
                break;
            case 3:
                Month = Month.AddMonths(1);
                break;
            case 4:
                Month = Month.AddYears(1);
                break;
        }

        await MonthChanged.InvokeAsync(Month);
        Refresh();
    }
}
